#include "Ks.h"


Ks::Ks()
{
}
Ks::Ks(ks_arch arch, int mode)
{
	_arch = arch;
	_mode = mode;

}

Ks::~Ks()
{

}

int Ks::asm_code(char * code, unsigned char * bytes, size_t* size)
{
	ks_engine *ks;
	ks_err err;
	size_t count;
	unsigned char *encode;
	
	HMODULE hDll = LoadLibrary("./keystone.dll");
	if (hDll != NULL)
	{
		ptr_ks_open ksopen = (ptr_ks_open)GetProcAddress(hDll, "ks_open");
		ptr_ks_asm  ksasm = (ptr_ks_asm)GetProcAddress(hDll, "ks_asm");
		ptr_ks_free ksfree = (ptr_ks_free)GetProcAddress(hDll, "ks_free");
		ptr_ks_close ksclose = (ptr_ks_close)GetProcAddress(hDll, "ks_close");
		err = ksopen(_arch, _mode, &ks);
		if (err != KS_ERR_OK) 
		{
			printf("ERROR: failed on ks_open(), quit\n");
			return -1;
		}

		if (ksasm(ks, code, 0, &encode, size, &count) != KS_ERR_OK)
		{
			count = 0;
		}
		else
		{
			memcpy(bytes,encode,*size);
		}
		ksfree(encode);
		ksclose(ks);
	}

	
	return count;
}



